package tech.guanlin.com.kafka.study.producer;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import tech.guanlin.com.kafka.study.message.Message;

import java.util.Date;

@Component
@SuppressWarnings("all")
public class KafkaSender {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    private Gson gson = new GsonBuilder().create();

    //发送消息方法
    public void send() {
        Message message = new Message();
        message.setMsg("我是测试内容"+System.currentTimeMillis());
        message.setId(System.currentTimeMillis());
        //message.setMsg(UUID.randomUUID().toString());
        message.setSendTime(new Date());
        kafkaTemplate.send("test", gson.toJson(message));
        System.out.println("+++++++++++++++++++++  发送message = {}"+gson.toJson(message));
    }
}
